package top.happylaishop.shop.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import top.happylaishop.shop.dto.CurrentUserDto;
import top.happylaishop.shop.dto.UserDto;
import top.happylaishop.shop.exception.NotAuthrizationException;
import top.happylaishop.shop.service.CommonService;
import top.happylaishop.shop.utils.CommonUtils;

@Service
public class CommonServiceImpl implements CommonService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public CurrentUserDto getCurrentUser() {
        String token = CommonUtils.getToken();
        if(StrUtil.isBlank(token)){
            throw new NotAuthrizationException();
        }
        return JSON.parseObject(redisTemplate.opsForValue().get(CommonUtils.generateLoginToken(token)), CurrentUserDto.class);
    }

    @Override
    public UserDto getCurrentUserInfo() {
        CurrentUserDto currentUser = getCurrentUser();
        if(currentUser != null){
            return currentUser.getUserDto();
        }
        return null;
    }


}
